package config

import (
	"fmt"
	"github.com/gofiber/fiber/v2/log"
	"os"

	"github.com/joho/godotenv"
)

type Application struct {
	DBHost     string
	DBPort     string
	DBUser     string
	DBPassword string
	DBName     string

	RedisHost     string
	RedisPort     string
	RedisPassword string
	RedisDb       string

	LogLevel string
	SECRET   string
}

var App *Application

func InitConfig() {
	a := Application{

		DBHost:     GetEnv("DB_HOST", "127.0.0.1"),
		DBPort:     GetEnv("DB_PORT", "3306"),
		DBUser:     GetEnv("DB_USER", "root"),
		DBName:     GetEnv("DB_NAME", "fiber"),
		DBPassword: GetEnv("DB_PASSWORD", "123456"),

		RedisHost:     GetEnv("REDIS_HOST", "10.16.168.61"),
		RedisPort:     GetEnv("REDIS_PORT", "6379"),
		RedisDb:       GetEnv("REDIS_DB", "1"),
		RedisPassword: GetEnv("REDIS_PASSWORD", "123456"),

		LogLevel: GetEnv("LOG_LEVEL", "debug"),
		SECRET:   GetEnv("SECRET", "12345678"),
	}

	App = &a
}

// GetEnv func to get env value
func GetEnv(key string, defaultValue string) string {
	// load .env file
	err := godotenv.Load(".env")
	if err != nil {
		fmt.Print("Error loading .env file")
	}
	value := os.Getenv(key)
	if value == "" {
		log.Warn("参数为空", key)

		return defaultValue
	}
	return value
}
